package com.thiswhyme.demo.msgpush.core.impl.websocket.event;

import com.thiswhyme.demo.msgpush.core.event.AbstractConnectEventWsHandler;
import com.thiswhyme.demo.msgpush.core.event.bean.WebSocketEvent;
import com.thiswhyme.demo.msgpush.core.impl.websocket.SessionManager;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * @description: WebSocket连接建立事件处理器-ws实现
 * @author: thiswhyme
 * @date: 2021/9/28
 */
@Slf4j
public class ConnectEventWsHandler extends AbstractConnectEventWsHandler {

    @Override
    public void doHandle(WebSocketEvent event) {
        event.getIdentifiers().forEach(identifier -> {
            Optional.ofNullable(SessionManager.get(identifier)).ifPresent(session -> {
                if (!session.getId().equals(event.getMsgContent())) {
                    SessionManager.removeOtherSession(identifier, event.getMsgContent());
                    log.info("用户【{}】再次登录，剔除之前的会话", identifier);
                }
            });
        });
    }
}
